<?php

namespace magein\utils;

/**
 * 根据数据表生成apipost文档
 */
class Apipost
{

    /**
     * 通过show full columns from $table_name 获取到的属性
     * @var array
     */
    protected array $attrs = [];

    /**
     * @var string
     */
    protected string $properties = '';

    /**
     * @var string
     */
    protected string $fields = '';

    /**
     *
     * @param array $attrs
     */
    public function __construct(array $attrs)
    {
        $this->attrs = $attrs;
        $this->parseTableAttrs($attrs);
    }

    /**
     * @param array $attrs
     * @return array
     */
    public function parseTableAttrs(array $attrs): array
    {
        $properties = "";
        $fields = '';

        foreach ($attrs as $attr) {

            $field = $attr['Field'];
            if (in_array($field, ['updated_at', 'deleted_at'])) {
                continue;
            }

            $comment = $attr['Comment'];
            $type = $attr['Type'];

            $comment = trim($comment);
            $comment = explode(' ', $comment);
            $comment = current($comment);

            if (str_contains($type, 'int')) {
                $type = 'integer';
            } elseif (str_contains($type, 'decimal')) {
                $type = 'number';
            } elseif (str_contains($type, 'double')) {
                $type = 'number';
            } elseif (str_contains($type, 'float')) {
                $type = 'number';
            } else {
                $type = 'string';
            }

            $properties .= <<<EOF
          "$field": {
            "type": "$type",
            "mock": {
              "mock": "@empty()"
            },
            "description": "$comment"
		  },
EOF;
            $properties .= "\n";

            $fields .= <<<EOF
          "$field",
EOF;
            $fields .= "\n";
        }

        $properties = trim($properties);
        $properties = trim($properties, ',');
        $fields = trim($fields);
        $fields = trim($fields, ',');

        $this->properties = $properties;
        $this->fields = $fields;

        return compact('properties', 'fields');
    }

    /**
     * @return string
     */
    public function object(): string
    {
        $properties = $this->properties;
        $fields = $this->fields;

        return <<<EOF
{
  "type": "object",
  "properties": {
    "code": {
      "type": "integer",
      "description": "请求状态",
      "mock": {
        "mock": "1"
      }
    },
    "msg": {
      "type": "string",
      "description": "描述信息",
      "mock": {
        "mock": "@empty()"
      }
    },
    "data": {
      "type": "object",
      "properties": {
        $properties
      },
      "APIPOST_ORDERS": [
        $fields
      ],
      "required": []
    }
  },
  "APIPOST_ORDERS": [
    "code",
    "msg",
    "data"
  ],
  "required": []
}
EOF;
    }

    /**
     * @return string
     */
    public function page(): string
    {
        $properties = $this->properties;
        $fields = $this->fields;

        return <<<EOF
{
  "type": "object",
  "properties": {
    "code": {
      "type": "integer",
      "description": "请求状态",
      "mock": {
        "mock": "1"
      }
    },
    "msg": {
      "type": "string",
      "description": "描述信息",
      "mock": {
        "mock": "@empty()"
      }
    },
    "data": {
      "type": "object",
      "properties": {
        "page": {
          "type": "object",
          "properties": {
            "page_id": {
              "type": "integer",
              "mock": {
                "mock": "@empty()"
              },
              "description": "当前页"
            },
            "page_size": {
              "type": "integer",
              "mock": {
                "mock": "@empty()"
              },
              "description": "每页数量"
            },
            "last_page": {
              "type": "integer",
              "mock": {
                "mock": "@empty()"
              },
              "description": "每页数量"
            },
            "total": {
              "type": "integer",
              "mock": {
                "mock": "@empty()"
              },
              "description": "总数"
            },
            "has_more": {
              "type": "boolean",
              "mock": {
                "mock": "@empty()"
              },
              "description": "是否还有更多 false 表示没有更多了"
            }
          },
          "APIPOST_ORDERS": [
            "page_id",
            "page_size",
            "last_page",
            "total",
            "has_more"
          ],
          "required": []
        },
        "lists": {
          "type": "array",
          "items": {
            "type": "object",
            "title": "Items",
            "properties": {
              $properties
            },
            "APIPOST_ORDERS": [
              $fields
            ],
            "required": []
          }
        }
      },
      "APIPOST_ORDERS": [
        "page",
        "lists"
      ],
      "required": []
    }
  },
  "APIPOST_ORDERS": [
    "code",
    "msg",
    "data"
  ],
  "required": []
}
EOF;
    }

    /**
     * @return string
     */
    public function lists(): string
    {
        $properties = $this->properties;
        $fields = $this->fields;

        return <<<EOF
{
  "type": "object",
  "properties": {
    "code": {
      "type": "integer",
      "description": "请求状态",
      "mock": {
        "mock": "1"
      }
    },
    "msg": {
      "type": "string",
      "description": "描述信息",
      "mock": {
        "mock": "@empty()"
      }
    },
    "data": {
      "type": "array",
      "description": "数据",
      "items": {
        "type": "object",
        "title": "Items",
        "description": "",
        "properties": {
          $properties
        },
        "APIPOST_ORDERS": [
          $fields
        ],
        "required": []
      }
    }
  },
  "APIPOST_ORDERS": [
    "code",
    "msg",
    "data"
  ],
  "required": []
}
EOF;
    }
}